package cn.dlc.com.system.user.controller;

import cn.dlc.com.base.controller.BaseController;
import cn.dlc.com.config.response.ResponseHttpStatus;
import cn.dlc.com.config.response.ResponseVO;
import cn.dlc.com.system.user.dto.UserDTO;
import cn.dlc.com.system.user.entity.UserEntity;
import cn.dlc.com.system.user.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: dlc
 * @Date: 2021/11/19 - 9:54
 */
@RequestMapping("sys/user")
@RestController
@Api(tags="账号信息")
@Slf4j
public class UserController extends BaseController<UserEntity, UserDTO> {

    @Autowired
    private IUserService userService;


    /**
     * 分页
     * @param userDTO
     * @return 分页数据
     */
    @GetMapping("page")
    @ApiOperation("获取分页数据")
    public ResponseVO<IPage<UserEntity>> page(UserDTO userDTO){

        try {
            // 构造分页条件
            Page<UserEntity> page = new Page<>(userDTO.getPageNumber(), userDTO.getPageSize());
            UserEntity entity = new UserEntity();
            BeanUtils.copyProperties(userDTO, entity);

            // 获取分页数据
            QueryWrapper<UserEntity> params = new QueryWrapper<>(entity);
            // 构造排序
            buildQueryWrapper(params, userDTO);

            Page<UserEntity> result = userService.page(page,params);

            return  ResponseVO.success(result);

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 列表
     * @param userDTO
     * @return 列表数据
     */
    @GetMapping("list")
    @ApiOperation("获取列表数据")
    public ResponseVO<List<UserEntity>> list(UserDTO userDTO){

        try {
            // 构造条件
            UserEntity entity = new UserEntity();
            BeanUtils.copyProperties(userDTO, entity);

            // 获取分页数据
            QueryWrapper<UserEntity> params = new QueryWrapper<>(entity);
            // 构造排序
            buildQueryWrapper(params, userDTO);

            List<UserEntity> list = userService.list(params);

            return ResponseVO.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取指定记录
     * @param id 主键
     * @return 当前对象
     */
    @GetMapping("{id}")
    @ApiOperation("根据id获取当前对象")
    public ResponseVO<UserEntity> findById(@ApiParam("主键") @PathVariable("id") String id){

        if(StringUtils.isNotBlank(id)) {
            UserEntity userEntity = userService.getById(id);
            return ResponseVO.success(userEntity);
        }

        return ResponseVO.error(ResponseHttpStatus.ARGS_ERROR);
    }

    /**
     * 保存
     * @param userDTO
     * @return 当前对象
     */
    @PostMapping
    @ApiOperation("保存当前对象")
    public ResponseVO<UserEntity> save(@RequestBody UserDTO userDTO){

        if(userDTO != null) {
            // 构造参数
            UserEntity userEntity = new UserEntity();
            BeanUtils.copyProperties(userDTO, userEntity);
            // 保存数据
            boolean saveResult = userService.save(userEntity);
            if(saveResult) {
                return ResponseVO.success(ResponseHttpStatus.SAVE_SUCCESS,userEntity);
            }
        }
        return ResponseVO.error(ResponseHttpStatus.SAVE_ERROR);
    }

    /**
     * 更新
     * @param userDTO
     * @return 当前对象
     */
    @PutMapping
    @ApiOperation("更新当前对象")
    public ResponseVO<UserEntity> update(@RequestBody UserDTO userDTO){

        if(userDTO != null) {
            // 构造参数
            UserEntity userEntity = new UserEntity();
            BeanUtils.copyProperties(userDTO, userEntity);
            // 更新
            boolean saveResult = userService.updateById(userEntity);
            if(saveResult) {
                return ResponseVO.success(ResponseHttpStatus.SAVE_SUCCESS,userEntity);
            }
        }
        return ResponseVO.error(ResponseHttpStatus.SAVE_ERROR);
    }

    /**
     * 删除指定记录
     * @param id 主键
     * @return 当前对象
     */
    @DeleteMapping("{id}")
    @ApiOperation("根据id删除当前对象")
    public ResponseVO<UserEntity> deleteById(@ApiParam("主键")@PathVariable("id") String id){

        if(StringUtils.isNotBlank(id)) {
            boolean removeResult = userService.removeById(id);
            if(removeResult) {
                return ResponseVO.success(ResponseHttpStatus.DELETE_SUCCESS);
            }
        }

        return ResponseVO.success(ResponseHttpStatus.DELETE_ERROR);
    }

    /**
     * 导出excel
     * @param userDTO
     * @return
     */
    @PostMapping("excel")
    @ApiOperation("导出excel")
    public void exportExcel(@RequestBody UserDTO userDTO){

        // 获取生成的文件，然后直接导出。



    }

    /**
     * 获取pdf，这个是不是放这里还待定，因为pdf可能放在文件服务器管理。
     * 如果要做权限控制的话，那么这个可能就需要通过这种方式实现。
     * @param id
     * @return pdf路径
     */
    @GetMapping("pdf/{id}")
    @ApiOperation("获取对应的pdf文件")
    public ResponseVO<String> exportPdf(@ApiParam("主键")@PathVariable("id") String id){

        if(StringUtils.isNotBlank(id)) {
            return ResponseVO.success("http://www.baidu.com");
        }
        return ResponseVO.error(ResponseHttpStatus.ARGS_ERROR);
    }




}
